//
//  NotificationTimeTableViewController.m
//  TableViewApp
//
//  Created by Dmitriy on 13.06.13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "NotificationTimeTableViewController.h"
#import "NotificationData.h"
#import "CustomNotificationCell.h"
#import "TaskEditingInputAccessoryView.h"

@implementation NotificationTimeTableViewController
const int SECONDS_IN_MINUTE = 60;
const int SECONDS_IN_HOUR = SECONDS_IN_MINUTE*60;
const int SECONDS_IN_DAY = SECONDS_IN_HOUR*24;

- (id)initWithCoder:(NSCoder *)aDecoder//почему не initWithNibName:Bundle?
{
    if (self = [super initWithCoder:aDecoder]) {
        timeCaptionsAndValues = [[NSArray arrayWithObjects:
                                 [NotificationData notificationDataWithCaption:@"In time" WithTimeInterval:0],
                                 [NotificationData notificationDataWithCaption:@"Before 5 min." WithTimeInterval:SECONDS_IN_MINUTE*5],
                                 [NotificationData notificationDataWithCaption:@"Before an hour" WithTimeInterval:SECONDS_IN_HOUR],
                                 [NotificationData notificationDataWithCaption:@"Before a day" WithTimeInterval:SECONDS_IN_DAY],
                                 nil] retain];//RETAIN???
    }
    return self;
}

-(void) viewDidUnload
{
    [taskForEditing release]; 
}

#pragma mark - UITableViewDelegate

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0)return 4;
    else return 1;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell;
    NSString* cellId;
    if (indexPath.section==0) {
        cellId = @"TimeCell";
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.textLabel.text = [(NotificationData*)[timeCaptionsAndValues objectAtIndex:indexPath.row] caption];
    }
    else
    {
        cellId = @"CustomNotification";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell==nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomNotificationCell" owner:self options:nil] objectAtIndex:0];
        }
        picker = [[TimeIntervalPicker alloc]init];
        picker.delegate = picker;
        picker.dataSource = picker;
        [picker setShowsSelectionIndicator:YES];
        [[(CustomNotificationCell*)cell timeIntervalField] setInputView:picker];//как убрать warning?
        TaskEditingInputAccessoryView *accessoryView = [[[NSBundle mainBundle]loadNibNamed:@"TaskEditingInputAccessoryView" owner:self options:nil]objectAtIndex:0];
        [accessoryView.doneButton addTarget:self action:@selector(dateIsEntered:) forControlEvents:UIControlEventTouchUpOutside];
        customNotificationField = [(CustomNotificationCell*)cell timeIntervalField];
        [[(CustomNotificationCell*)cell timeIntervalField] setInputAccessoryView:accessoryView]; 
    }
    return cell;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        NSTimeInterval notificationInterval = [(NotificationData*)[timeCaptionsAndValues objectAtIndex:indexPath.row] timeInterval];
        taskForEditing.notificationInterval = notificationInterval;
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [customNotificationField becomeFirstResponder];
    }
}
-(IBAction)dateIsEntered:(id)sender
{
    taskForEditing.notificationInterval = [picker interval];
    [picker release];
    [customNotificationField resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) setEditingTask:(Task*)task
{
    taskForEditing = [task retain];
}

@end
